<?php
class Report extends Vfriend_Db {
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Vfriend_Cache();
		$this->clear = $this->cache->clear;
	}
	public function get($intReportId, $isDetail=0) {
		try {
			$this->prepare('call reports_get(?)');
			$this->bindParam(1, $intReportId, PDO::PARAM_INT, 11);
			$arrData 	= $this->fetchAll();
			$arrResult 	= isset($arrData[0]) ? $arrData[0] : null;
			if($arrResult) {
				$intType	= $arrResult['type'];
				$intSrcId	= $arrResult['srcid'];
				if($intType == 1) {
					$objBlog			= new Blog();
					$arrResult['info']	= $objBlog->get($intSrcId);
				}elseif($intType == 2) {
					$objPhoto			= new Photo();
					$arrResult['info']	= $objPhoto->getPhoto(0,$intSrcId);
				}elseif($intType == 3) {
					$objFullhouse		= new Fullhouse();
					$arrResult['info']	= $objFullhouse->get($intSrcId);
				}elseif($intType == 4) {
					$objNew				= new News();
					$arrResult['info']	= $objNew->get($intSrcId);
				}elseif($intType == 5) {
					$objOffline			= new Offline();
					$arrResult['info']	= $objOffline->get($intSrcId);
				}elseif($intType == 7) {
					$objPhoto			= new Photo();
					$arrResult['info']	= $objPhoto->getAlbumInfo($intSrcId);
					if($isDetail) {
						$arrResult['img']	= $objPhoto->getImageForAlbum($intSrcId);
					}
				}elseif($intType == 8) {
					$objPlace			= new Place();
					$arrResult['info']	= $objPlace->get($intSrcId);
					
				}
			}
			return $arrResult;
		}catch(Exeception $e) {
			Vfriend_Function::logDb('Backend::Report::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAll($intType, $intStatus, $strStart, $strEnd, $intOff, $intLimit, &$intTotal=0) {
		try {			
			$this->prepare('call reports_getall(?,?,?,?,?,?)');
			$this->bindParam(1, $intType, PDO::PARAM_INT, 4);
			$this->bindParam(2, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(3, $strStart, PDO::PARAM_STR, 10);
			$this->bindParam(4, $strEnd, PDO::PARAM_STR, 10);
			$this->bindParam(5, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(6, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();	
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('Backend::Report::getAll - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateStatus($intAdminId, $intReportId, $intStatus, $intPerm, $intUserId) {
		try {
			$this->prepare('call reports_update_status(?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intReportId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;	
			if($intError == 0) {
				$this->clear->deleteUstat($intUserId);	
				$arrInfo = $this->get($intReportId);				
				$intType 	= $arrInfo['type'];
				$intSrcId	= $arrInfo['srcid'];				
				if($intType == 1) {
					$intOwnerId	= $arrInfo['info']['userid'];
					$this->clear->deleteBlogById($intSrcId);
					$this->clear->deleteBlog($intOwnerId);
				}elseif($intType == 2) {
					$intAlbumId	= $arrInfo['info']['albumid'];
					$this->clear->deleteAlbumByUserId($intAlbumId);
					$this->clear->deleteCmtForPhoto($intSrcId);
				}elseif($intType == 3) {
					$this->clear->deleteUblastById($intSrcId);
				}elseif($intType == 4) {
					$intOwnerId	= $arrInfo['info']['userid'];
					$intSubCate	= $arrInfo['info']['subcategoryid'];
					$this->clear->deleteNewsById($intSrcId);
					$this->clear->deleteNews($intSubCate,$intOwnerId);
				}elseif($intType == 5) {
					$intOwnerId		= $arrInfo['info']['userid'];
					$intCategoryId	= $arrInfo['info']['categoryid'];
					$this->clear->deleteOfflineByUserId($intOwnerId);
					$this->clear->deleteOfflineByCategory($intCategoryId);
					$this->clear->deleteOfflineById($intSrcId,$intOwnerId);
					$this->clear->deleteCmtByOfflineId($intSrcId);
				}elseif($intType == 6) {
				}elseif($intType == 7) {
					$this->clear->deleteAlbumByUserId($intSrcId);
				}elseif($intType == 8) {
					$intSubCate		= $arrInfo['info']['subcategoryid'];
					$intProvince	= $arrInfo['info']['province'];
					$this->clear->deletePlaces($intSubCate,$intProvince);
					$this->clear->deletePlaceById($intSrcId);
				}
				if($intStatus == 2 || $intStatus == 3) {
					set_include_path (
						ROOT_DIR .
						PATH_SEPARATOR . ROOT_DIR . '/frontend/models/' .
						PATH_SEPARATOR . get_include_path()
					);
					if($intStatus == 2) $intType = 1;
					else $intType = -1;
					
					$objNotification	= new Notification();
					$arrJson			= Zend_Json::encode(array('type'=>$arrInfo['type'],'info'=>$arrInfo['info']));
					$objNotification->insert($intUserId, $intType, $arrJson, $intReportId);
				}
			}
			return $intError;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('Backend::Report::updateStatus - ' .$e->getMessage());			
			return null;
		}
	}
	public function delete($intAdminId, $intReportId) {
		try {
			$this->prepare('call reports_delete(?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intReportId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;			
			return $intError;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('Backend::Report::delete - ' .$e->getMessage());			
			return null;
		}
	}
}
?>